package restaurant.behaviours.master;

import restaurant.agents.MasterWaiterAgent;
import restaurant.core.CustomizedPerformative;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

public class MasterSyncroBehaviour extends CyclicBehaviour {
	
	private static final long serialVersionUID = 5020291734443374263L;
	private MasterWaiterAgent agent;
	
	public MasterSyncroBehaviour(MasterWaiterAgent agent) {
		this.agent = agent;
	}

	@Override
	public void action() {
		MessageTemplate messageTemplate = MessageTemplate.MatchPerformative(CustomizedPerformative.SYNCRO);
		ACLMessage message = myAgent.receive(messageTemplate);
		if(message != null) {
			synchronized (this.agent.freeWaiters) {
				if(message.getContent().equals("YES")) {
					this.agent.freeWaiters.put(message.getSender(), true);
				} else {
					this.agent.freeWaiters.put(message.getSender(), false);
				}
			}
		}
	}

}